﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Lib
{
    public class DateTimeHelper
    {

        public static DateTime StartOfWeek(DateTime dt, DayOfWeek startOfWeek)
        {
            int diff = dt.DayOfWeek - startOfWeek;
            if (diff < 0)
            {
                diff += 7;
            }

            return dt.AddDays(-1 * diff).Date;
        }
        public static DateTime EndOfWeek(DateTime dt, DayOfWeek startOfWeek)
        {
            DateTime d = StartOfWeek(dt, startOfWeek).AddDays(6).AddHours(23).AddMinutes(59).AddSeconds(59);
            return d;
        }
        public static DateTime StartOfMonth(DateTime dt)
        {
            DateTime d = new DateTime(dt.Year, dt.Month, 1);
            return d;
        }
        public static DateTime EndOfMonth(DateTime dt)
        {
            int day = DateTime.DaysInMonth(dt.Year, dt.Month);
            DateTime d = new DateTime(dt.Year, dt.Month, day).AddHours(23).AddMinutes(59).AddSeconds(59);
            return d;
        }
        public static DateTime StartOfPeriod(DateTime dt)
        {
            int curMonth = dt.Month;
            int mod = curMonth / 3;
            DateTime d = new DateTime(dt.Year, mod*3 + 1, 1);
            return d;
        }
        public static DateTime EndOfPeriod(DateTime dt)
        {
            int curMonth = dt.Month;
            int mod = curMonth / 3;
            DateTime d = new DateTime(dt.Year, mod * 3 + 3, DateTime.DaysInMonth(dt.Year, mod * 3 + 3));
            return d;
        }
        public static int GetPeriod(DateTime dt)
        {
            int curMonth = dt.Month;
            int mod = curMonth / 3;
            return mod;
        }
    }
}
